/**
*  @file       timer.c
*  @brief      定时器配置库
*  @author     Z B T
*  @version	   LTE
*  @date       2015-10
*/

#include "timer.h"

/**
 * @brief:  定时器1中断服务程序 中断号 3
 * @para：  [in]timerx, [in]timer_x
 * @return：0：成功 1：空操作 2 错误
 */
static uint_8 timer_inilize(enum TIMER_N timer_n, struct TIMER_INIT_CONFIG timer_x)
{
	if (timer_n > timer_1)
	{
		return 1;			                                    /**< 空操作返回 1 */
	}

	if (timer_n == timer_0)
	{
		TR0 = 0;                                                /**< 关闭定时器 */
		if (timer_x.timerInterrupt == ENABLE)
		{
			ET0 = 1;                                             /**< 允许中断 */
		}
		else
		{
			ET0 = 0;
		}
		if (timer_x.timerMode > timer_8Bit)      
		{
			return 2;                                           /**< 初始化错误 返回 2 */
		}
		TMOD = (TMOD & ~ 0x03) | timer_x.timerMode;             /**< M1 M0位先清零 后置位 */
		TL0 = (uint_8)(timer_x.timerInitValue);
		TH0 = (uint_8)(timer_x.timerInitValue >> 8);
		if (timer_x.timerRun == ENABLE)
		{
			TR0 = 1;                                             /**< 启动定时器 */
		}
        EA |= 1;                                                 /**< 开启总中断 */
		return 0;                                                /**< 初始化配置成功 */
	}

	if (timer_n == timer_1)
	{
		TR1 = 0;
		if (timer_x.timerInterrupt == ENABLE)
		{
			ET1 = 1;
		}
		else
		{
			ET1 = 0;
		}
		if (timer_x.timerMode > timer_8BitAutoReload)
		{
			return 2;
		}
		TMOD = (TMOD & ~ 0x30) | timer_x.timerMode;              /**< M1 M0位先清零 后置位 */
		TL1 = (uint_8)(timer_x.timerInitValue);
		TH1 = (uint_8)(timer_x.timerInitValue >> 8);
		if (timer_x.timerRun == ENABLE)
		{
			TR1 = 1;
		}
        EA |= 1;        
		return 0;
	}

	return 2;                                                     /**< 错误 */
}
